Ext.ns('AIR2.UI.App');
/***************
 * AIR2 App Component
 *
 * Primary application used in AIR2.  Sets up title/project/location info at
 * the top of the page, and loads dragzones and the Drawer component.
 *
 * @class AIR2.UI.App
 * @extends Ext.Container
 * @xtype air2app
 * @cfg {Array} items
 *   Items to render in the body of the application.
 *
 */
AIR2.UI.App = function(config) {
    config.id = 'air2-app'; // ALWAYS set a static ID

    this.bufferEl = document.createElement('div');
    this.headerEl = Ext.get('air2-header');
    this.locationEl = Ext.get('air2-toolbar-location');

    // recent buttons
    this.addRecentButton({
        view: 'project',
        iconCls: 'air2-icon-project',
        data: AIR2.RECENT.project,
        uuid: 'prj_uuid',
        textFn: function(data) {return data.prj_display_name}
    });
    this.addRecentButton({
        view: 'source',
        iconCls: 'air2-icon-source',
        data: AIR2.RECENT.source,
        uuid: 'src_uuid',
        textFn: function(data) {return AIR2.Format.sourceName(data)}
    });
    this.addRecentButton({
        view: 'inquiry',
        iconCls: 'air2-icon-inquiry',
        data: AIR2.RECENT.inquiry,
        uuid: 'inq_uuid',
        textFn: function(data) {return AIR2.Format.inquiryTitle(data)}
    });
    this.addRecentButton({
        view: 'submission',
        iconCls: 'air2-icon-responses',
        data: AIR2.RECENT.submission,
        uuid: 'srs_id',
        textFn: function(data) {
            var s = AIR2.Format.sourceName(data) + ' - ';
            s += Ext.util.Format.ellipsis(data.inq_ext_title, 50);
            return s;
        }
    });

    // account button
    new AIR2.UI.Button({
        air2type: 'DARKER',
        iconCls: 'air2-icon-chevron',
        menuAlign: 'tr-br?',
        menu: {
            cls: 'air2-header-menu',
            showSeparator: false,
            items: [
                {text: 'Manage Profile', href: AIR2.HOMEURL + '/user/' + AIR2.USERINFO.uuid},
                {text: 'Logout', href: AIR2.LOGOUTURL}
            ]
        },
        renderTo: this.bufferEl,
        listeners: {
            render: function(btn) {
                var prev = this.headerEl.child('.account .air2-btn');
                btn.el.replace(prev);
            },
            scope: this
        }
    });

    // search buttons
    var form = Ext.get('air2-search-form');
    var stype = new AIR2.UI.Button({
        air2type: 'BLUE',
        iconCls: 'air2-icon-'+AIR2.SEARCHIDX,
        menu: {
            cls: 'air2-header-menu search',
            showSeparator: false,
            defaults: {
                xtype: 'menucheckitem',
                group: 'air2-search-header',
                clickHideDelay: 200,
                checkHandler: function(item, checked) {
                    if (checked) {
                        stype.setIconClass(item.iconCls);
                        form.dom.setAttribute('action', item.viewUrl);
                    }
                }
            },
            items: [
                {text: 'Active Sources', view: 'active-sources'},
                {text: 'All Sources', view: 'sources'},
                {text: 'Projects', view: 'projects'},
                {text: 'Queries', view: 'inquiries'},
                {text: 'Submissions', view: 'responses'},
            ]
        },
        renderTo: this.bufferEl,
        listeners: {
            render: function(btn) {
                // fix submit button
                form.addKeyListener(Ext.EventObject.ENTER, function(key, ev) {
                    ev.stopEvent();
                    form.dom.submit();
                });

                // configure menu items
                btn.menu.items.each(function(item) {
                    item.iconCls = 'air2-icon-'+item.view;
                    item.checked = (item.view == AIR2.SEARCHIDX);
                    item.viewUrl = AIR2.HOMEURL+'/search/'+item.view;
                });
                var el = this.headerEl.child('.search .x-btn-icon');
                btn.el.replace(el);
            },
            scope: this
        }
    });
    new AIR2.UI.Button({
        air2type: 'BLUE',
        text: 'Search',
        renderTo: this.bufferEl,
        handler: function() {
            form.dom.submit();
        },
        listeners: {
            render: function(btn) {
                var el = this.headerEl.child('.search .x-btn-noicon');
                btn.el.replace(el);
            },
            scope: this
        }
    });

    // add floating bin slide-out
    this.drawer = new AIR2.Drawer();

    // call parent constructor
    AIR2.UI.App.superclass.constructor.call(this, config);
}
Ext.extend(AIR2.UI.App, Ext.Container, {
    renderTo: 'air2-body',
    listeners: {
        render: function(comp) {
            comp.air2DragZone = new AIR2.Drawer.DragZone(Ext.getBody(), {
                getDragData: function(event) {
                    var el = event.getTarget('.air2-dragzone');

                    // check for hovering over the dragarrow
                    if (!el && event.getTarget('.air2-dragarrow')) {
                        el = AIR2.Drawer.hoverEl.ddEl;
                    }

                    if (el) {
                        return {
                            repairXY: Ext.fly(el).getXY(),
                            ddel: el, //string OR origin html element (to be cloned)
                            ddBatchType: el.getAttribute('air2type'),
                            selections: [{
                                uuid: el.getAttribute('air2uuid'),
                                reltype: el.getAttribute('air2rel')
                            }] //array of UUID's OR function returning array
                        }
                    }
                }
            });
        }
    },
    addRecentButton: function(opt) {
        // get menu items
        var items = [];
        for (var i=0; i<opt.data.length; i++) {
            items.push({
                text: opt.textFn(opt.data[i]),
                href: AIR2.HOMEURL+'/'+opt.view+'/'+opt.data[i][opt.uuid]
            });
        }
        if (items.length < 1) items = [{text: '(none)', disabled: true}];

        // create button
        new AIR2.UI.Button({
            air2type: 'DARKER',
            iconCls: opt.iconCls,
            menuAlign: 'tl-bl?',
            menu: {
                cls: 'air2-header-menu',
                showSeparator: false,
                items: items
            },
            renderTo: this.bufferEl,
            listeners: {
                render: function(btn) {
                    var el = this.headerEl.child('.recent-stuff .'+opt.iconCls);
                    btn.el.replace(el.parent());
                },
                scope: this
            }
        });
    },
    showProject: function(name, href) {
        // TODO: remove usages
    },
    hideProject: function() {
        // TODO: remove usages
    },
    showLocation: function(name, iconCls, href, dragType, uuid, dragRel) {
        if (!href)
            this.locationEl.first().update(name);
        else
            this.locationEl.update('<a href="'+href+'">'+name+'</a>');

        if (iconCls) {
            if (this.locationEl.iconClass)
                this.locationEl.first().removeClass(this.locationEl.iconClass);
            this.locationEl.first().addClass(iconCls);
            this.locationEl.iconClass = iconCls;
        }

        if (dragType && uuid) {
            this.locationEl.addClass('air2-dragzone');
            this.locationEl.set({
                air2type: dragType,
                air2uuid: uuid
            });
            if (dragRel) this.locationEl.set({air2rel: dragRel});
        }

        this.locationEl.show();
    },
    hideLocation: function() {
        this.locationEl.hide();
    },
    showDeleteFlag: function() {
        // TODO: remove usages
    },
    hideDeleteFlag: function() {
        // TODO: remove usages
    }
});
Ext.reg('air2app', AIR2.UI.App);
